﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.Organization
{
    public partial class OrganizationService : IOrganizationService
    {
        public bool SaveNode(Node node)
        {
            using (OrganizationDbContext context = new OrganizationDbContext())
            {
                if (!EntityRepository.SaveCodeEntity(node, context))
                {
                    return false;
                }
                return EntityRepository.Commit(context);
            }
        }

        public bool DeleteNode(Node node)
        {
            using (OrganizationDbContext context = new OrganizationDbContext())
            {
                if (!EntityRepository.DeleteCodeEntity(node, context))
                {
                    return false;
                }
                return EntityRepository.Commit(context);
            }
        }

        public Node GetNode(Guid id, bool isRecursive)
        {
            using (OrganizationDbContext context = new OrganizationDbContext())
            {
                Node model = context.Set<Node>().Where(p => p.Id == id).SingleOrDefault();

                if (isRecursive && model != null)
                {
                    Stack<Node> stack = new Stack<Node>();
                    stack.Push(model);

                    while (stack.Count > 0)
                    {
                        Node parentItem = stack.Pop();
                        parentItem.Children = context.Set<Node>().Where(p => p.ParentId.Value == parentItem.Id).ToList();
                        foreach (var item in parentItem.Children)
                        {
                            stack.Push(item);
                        }
                    }
                }

                return model;
            }
        }

        public Node GetNode(string code, bool isRecursive)
        {
            using (OrganizationDbContext context = new OrganizationDbContext())
            {
                Node model = context.Set<Node>().Where(p => p.Code == code).SingleOrDefault();

                if (isRecursive && model != null)
                {
                    Stack<Node> stack = new Stack<Node>();
                    stack.Push(model);

                    while (stack.Count > 0)
                    {
                        Node parentItem = stack.Pop();
                        parentItem.Children = context.Set<Node>().Where(p => p.ParentId.Value == parentItem.Id).ToList();
                        foreach (var item in parentItem.Children)
                        {
                            stack.Push(item);
                        }
                    }
                }

                return model;
            }
        }

        public List<Node> GetChildren(Guid id, bool isRecursive)
        {
            using (OrganizationDbContext context = new OrganizationDbContext())
            {
                List<Node> models = context.Set<Node>().Where(p => p.ParentId.Value == id).ToList();

                if (isRecursive && models != null)
                {
                    Stack<Node> stack = new Stack<Node>();
                    for (int i = 0; i < models.Count; i++)
                    {
                        stack.Push(models[i]);
                    }

                    while (stack.Count > 0)
                    {
                        Node parentItem = stack.Pop();
                        parentItem.Children = context.Set<Node>().Where(p => p.ParentId.Value == parentItem.Id).ToList();
                        foreach (var item in parentItem.Children)
                        {
                            stack.Push(item);
                        }
                    }
                }

                return models;
            }
        }

        public List<Node> GetChildren(string code, bool isRecursive)
        {
            using (OrganizationDbContext context = new OrganizationDbContext())
            {
                List<Node> models = context.Set<Node>().Where(p => p.Parent.Code == code).ToList();

                if (isRecursive && models != null)
                {
                    Stack<Node> stack = new Stack<Node>();
                    for (int i = 0; i < models.Count; i++)
                    {
                        stack.Push(models[i]);
                    }

                    while (stack.Count > 0)
                    {
                        Node parentItem = stack.Pop();
                        parentItem.Children = context.Set<Node>().Where(p => p.ParentId.Value == parentItem.Id).ToList();
                        foreach (var item in parentItem.Children)
                        {
                            stack.Push(item);
                        }
                    }
                }

                return models;
            }
        }
    }
}
